﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using System.Collections.ObjectModel;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;
using ChinaCustoms.Qingdao.Workflow.Controls;

public partial class Utility_OrganizationSelector : System.Web.UI.Page
{
    private IList<TreeNode> selectedNodes;
    /// <summary>
    /// 被选中的节点
    /// </summary>
    public IList<TreeNode> SelectedNodes
    {
        get { return selectedNodes; }
        set { selectedNodes = value; }
    }

    /// <summary>
    /// 是否仅仅显示到相对顶级部门
    /// </summary>
    private bool IsOnlyDisplayRelativeTopLevel = false;

    /// <summary>
    /// 需要显示的顶级部门的ID
    /// </summary>
    private string ParentOrganizationID = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        ParentOrganizationID = this.Request.QueryString["ParentOrganizationID"];
        bool tempIsOnlyDisplayTopLevel = false;
        bool.TryParse(this.Request.QueryString["IsOnlyDisplayRelativeTopLevel"], out tempIsOnlyDisplayTopLevel);
        IsOnlyDisplayRelativeTopLevel = tempIsOnlyDisplayTopLevel;
        if (this.IsPostBack == false)
        {
            this.BuildeTree();
        }
    }

    protected void SelectButton_Click(object sender, EventArgs e)
    {
        this.selectedNodes = new List<TreeNode>();
        string selectedNameArray = string.Empty;
        string selectedIDArray = string.Empty;
        this.GetSelectedNodes(this.TreeView1.Nodes[0]);
        for (int i = 0; i < selectedNodes.Count; i++)
        {
            selectedIDArray += string.Format("{0},", selectedNodes[i].Value);
            selectedNameArray += string.Format("{0},", selectedNodes[i].Text);
        }

        if (selectedIDArray.EndsWith(","))
        {
            selectedIDArray = selectedIDArray.Substring(0, selectedIDArray.Length - 1);
        }

        if (selectedNameArray.EndsWith(","))
        {
            selectedNameArray = selectedNameArray.Substring(0, selectedNameArray.Length - 1);
        }

        string returnValue = string.Format("\"{0}|{1}\"", selectedNameArray, selectedIDArray);
        ModalUtil.ClosePage(Page, returnValue);
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        ModalUtil.ClosePage(Page);
    }

    private void GetSelectedNodes(TreeNode parentNode)
    {
        if (parentNode.Checked == true)
        {
            selectedNodes.Add(parentNode);
        }

        if (parentNode.ChildNodes.Count > 0)
        {
            foreach (TreeNode tempnode in parentNode.ChildNodes)
            {
                GetSelectedNodes(tempnode);
            }
        }
    }

    private void BuildeTree()
    {
        this.TreeView1.ShowLines = true;
        this.TreeView1.ExpandDepth = 1;
        this.TreeView1.Nodes.Clear();
        this.TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
        IOrganization rootOrg = null;
        if (string.IsNullOrEmpty(this.ParentOrganizationID))
        {
            rootOrg = ChinaCustomsPermissionWrapper.GetRootOrganization();
        }
        else
        {
            rootOrg = ChinaCustomsPermissionWrapper.GetOrganization(ParentOrganizationID);
        }

        Dictionary<string, IOrganization> organizationCollectionAll = null;
        Dictionary<string, string> needDisplayOrg = null;
        ChinaCustomsPermissionWrapper.GetObjectsSearched(rootOrg, out needDisplayOrg, out organizationCollectionAll);

        if (rootOrg != null)
        {
            TreeNode rootNode = new TreeNode();
            rootNode.Value = rootOrg.ID;
            rootNode.Text = rootOrg.Name;
            rootNode.SelectAction = TreeNodeSelectAction.Expand;
            this.TreeView1.Nodes.Add(rootNode);
            this.BuildeTreeNode(rootNode, needDisplayOrg, organizationCollectionAll);
        }
    }

    private void BuildeTreeNode(TreeNode parentNode, Dictionary<string, string> needDisplayOrg, Dictionary<string, IOrganization> organizationCollectionAll)
    {
        if (needDisplayOrg == null || organizationCollectionAll == null)
        {
            return;
        }
        else
        {
            string parentID = parentNode.Value;

            foreach (KeyValuePair<string, string> kvpOrg in needDisplayOrg)
            {
                if (kvpOrg.Value == parentID)
                {
                    if (organizationCollectionAll.ContainsKey(kvpOrg.Key))
                    {
                        if (this.IsOnlyDisplayRelativeTopLevel == true)
                        {
                            if (organizationCollectionAll[kvpOrg.Key].IsTopOU == true)
                            {
                                TreeNode node = new TreeNode();
                                node.Value = kvpOrg.Key;
                                node.Text = organizationCollectionAll[kvpOrg.Key].DisplayName;
                                node.SelectAction = TreeNodeSelectAction.Expand;

                                this.BuildeTreeNode(node, needDisplayOrg, organizationCollectionAll);
                                parentNode.ChildNodes.Add(node);
                            }
                        }
                        else
                        {
                            TreeNode node = new TreeNode();
                            node.Value = kvpOrg.Key;
                            node.Text = organizationCollectionAll[kvpOrg.Key].DisplayName;
                            node.SelectAction = TreeNodeSelectAction.Expand;

                            this.BuildeTreeNode(node, needDisplayOrg, organizationCollectionAll);
                            parentNode.ChildNodes.Add(node);
                        }
                    }
                }
            }
        }
    }
}
